﻿using UnityEngine;
using System.Collections;

public class Popup_QuestComplete : Popup_Base {
	public GameObject[] rewardObjectList;
	public UILabel questTitleLabel;
	public UIGrid itemGrid;

	public UISprite rewardImage;
	public UILabel[] rewardAmountLabel;
    public UILabel rewardItemName;
    public GameObject[] rewardItemRareStar;
	
	public Transform itemObjectListTrans;

    public override void StartInit()
    {
        base.StartInit();

        SoundController.PlaySound("UI_popup_reward");
    }

	public void SetItemInfos()
	{
		int rewardCount = 0;
		int questId = UserData.Instance.userModel.questInfo.questId;
		DQuestInfo questInfo = GameData.Inst.GetQuestInfo(questId);

		questTitleLabel.text = TextController.GetStringWithId(string.Format("questTitle{0}", questId));
		if( questInfo.reward1Type != 0 )
		{
			rewardObjectList[questInfo.reward1Type-1].SetActive(true);
			if( questInfo.reward1Type < 3 )
				rewardAmountLabel[questInfo.reward1Type-1].text = questInfo.reward1Value1.ToString();
            else if (questInfo.reward1Type == 4)
			{
                //rewardImage.spriteName = TextController.GetItemDefine(questInfo.reward1Value1);
                WeaponAtlasController.SetWeaponSprite(rewardImage, questInfo.reward1Value1);
                rewardItemName.text = TextController.GetItemName(questInfo.reward1Value1);
                rewardItemRareStar[GameData.Inst.GetItemRarity(questInfo.reward1Value1)].SetActive(true);
                if (questInfo.reward1Value2 > 1)
                    rewardAmountLabel[questInfo.reward1Type - 1].text = string.Format("x{0}", questInfo.reward1Value2.ToString());
                else
                    rewardAmountLabel[questInfo.reward1Type - 1].gameObject.SetActive(false);
			}
            rewardObjectList[questInfo.reward1Type - 1].gameObject.SetActive(true);
			rewardCount++;
		}
		if( questInfo.reward2Type != 0 )
		{
			rewardObjectList[questInfo.reward2Type-1].SetActive(true);
			if( questInfo.reward2Type < 3 )
				rewardAmountLabel[questInfo.reward2Type-1].text = questInfo.reward2Value1.ToString();
            else if (questInfo.reward2Type == 4)
			{
                //rewardImage.spriteName = TextController.GetItemDefine(questInfo.reward2Value1);
                WeaponAtlasController.SetWeaponSprite(rewardImage, questInfo.reward2Value1);
                rewardItemName.text = TextController.GetItemName(questInfo.reward2Value1);
                rewardItemRareStar[GameData.Inst.GetItemRarity(questInfo.reward2Value1)].SetActive(true);
                if (questInfo.reward2Value2 > 1)
                    rewardAmountLabel[questInfo.reward2Type - 1].text = string.Format("x{0}", questInfo.reward2Value2.ToString());
                else
                    rewardAmountLabel[questInfo.reward2Type - 1].gameObject.SetActive(false);
			}
            rewardObjectList[questInfo.reward1Type - 1].gameObject.SetActive(true);
			rewardCount++;
		}
		if( questInfo.reward3Type != 0 )
		{
			rewardObjectList[questInfo.reward3Type-1].SetActive(true);
			if( questInfo.reward3Type < 3 )
				rewardAmountLabel[questInfo.reward3Type-1].text = questInfo.reward3Value1.ToString();
            else if (questInfo.reward3Type == 4)
			{
                //rewardImage.spriteName = TextController.GetItemDefine(questInfo.reward3Value1);
                WeaponAtlasController.SetWeaponSprite(rewardImage, questInfo.reward3Value1);
                rewardItemName.text = TextController.GetItemName(questInfo.reward3Value1);
                rewardItemRareStar[GameData.Inst.GetItemRarity(questInfo.reward3Value1)].SetActive(true);
                if (questInfo.reward3Value2 > 1)
                    rewardAmountLabel[questInfo.reward3Type - 1].text = string.Format("x{0}", questInfo.reward3Value2.ToString());
                else
                    rewardAmountLabel[questInfo.reward3Type - 1].gameObject.SetActive(false);
			}
            rewardObjectList[questInfo.reward1Type - 1].gameObject.SetActive(true);
			rewardCount++;
		}
		if( questInfo.reward4Type != 0 )
		{
			rewardObjectList[questInfo.reward4Type-1].SetActive(true);
			if( questInfo.reward4Type < 3 )
				rewardAmountLabel[questInfo.reward4Type-1].text = questInfo.reward4Value1.ToString();
            else if (questInfo.reward4Type == 4)
			{
                //rewardImage.spriteName = TextController.GetItemDefine(questInfo.reward4Value1);
                WeaponAtlasController.SetWeaponSprite(rewardImage, questInfo.reward4Value1);
                rewardItemName.text = TextController.GetItemName(questInfo.reward4Value1);
                rewardItemRareStar[GameData.Inst.GetItemRarity(questInfo.reward4Value1)].SetActive(true);
                if (questInfo.reward4Value2 > 1)
                    rewardAmountLabel[questInfo.reward4Type - 1].text = string.Format("x{0}", questInfo.reward4Value2.ToString());
                else
                    rewardAmountLabel[questInfo.reward4Type - 1].gameObject.SetActive(false);
			}
            rewardObjectList[questInfo.reward1Type - 1].gameObject.SetActive(true);
			rewardCount++;
		}
				
		itemGrid.GetComponent<UIGrid>().Reposition();

		Vector3 pos = itemGrid.transform.localPosition;
		if( rewardCount == 2 )
			pos.x = -160f;
		else if( rewardCount == 3 )
			pos.x = -320f;
		else if( rewardCount == 4 )
			pos.x = -480f;
		itemGrid.transform.localPosition = pos;
	}

	public void ClosePopup()
	{
		NetworkManager.Instance.C2S_GET_QUESTINFO(ResponseGetQuestInfo);
	}

	private void ResponseGetQuestInfo(ResponseModel<QuestResponseModel> result)
	{
        PlayerPrefs.SetInt("questVisit", 0);
		QuestManager.instance.isAlreadyShownCompleteToast = false;
		UserData.Instance.userModel.questInfo = result.data;
		PopupManager.PopPopup();
		SceneController.LoadLevel("Main");
	}
}
